package com.yincai.carefreewater.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.yincai.carefreewater.R;
import com.yincai.carefreewater.base.BaseActivity;
import com.yincai.carefreewater.tool.JumpTool;
import com.yincai.carefreewater.tool.ShareTool;
import com.yincai.carefreewater.tool.ToastTool;
import com.yincai.carefreewater.tool.WindowTool;
import com.yincai.carefreewater.usb.UsbTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PaidShuiActivity extends BaseActivity {
    @BindView(R.id.head_close)
    ImageView headClose;
    @BindView(R.id.paishui_progress)
    ProgressBar paishuiProgress;
    int ps_pro=0;
    private String msg_send="";
    private String activity="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paid_shui);
        ButterKnife.bind(this);
        UsbTool.getInstance().setHandler(usbHandler);
        UsbTool.getInstance().openUsbDevice(PaidShuiActivity.this);
        Log.i(TAG, "onCreate: "+usbHandler);
        activity=getIntent().getStringExtra("activity");
    }



    Handler ps_handler=new Handler();
    int ps_i = 5 * 60 * 1000 / 100;
    Runnable runnable1=new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            if(ps_pro<100) {
                ps_handler.postDelayed(this, ps_i);
                ps_pro++;
                paishuiProgress.setProgress(ps_pro);
                ShareTool.saveInfo(PaidShuiActivity.this,"progress",ps_pro+"");
            }
        }
    };
    private Handler usbHandler =new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            String str=msg.obj.toString();
            switch (msg.what) {
                case -1:
                    ToastTool.toast(str);
                    break;
                case 0:
                    ToastTool.toast(str);
                    break;
                case 1:
                    try {
                        if(str.contains("AT+OK")){
                            if(msg_send.contains("=")){
                                msg_send=msg_send.substring(0,msg_send.indexOf("="));
                            }
                            switch (msg_send){
                                case  "AT+CS4":
                                    ToastTool.toast("凉水水水量设置成功");
                                    break;
                                case  "AT+CS2":
                                    ToastTool.toast("热水水量设置成功");
                                    break;
                                case  "AT+CZ":
                                    ToastTool.toast("复位");
                                    break;
                                case  "AT+BEGIN":
                                    ToastTool.toast("设备启用");
                                    break;
                                case  "AT+END":
                                    ToastTool.toast("设备停用");
                                    break;
                                case  "AT+STOP":
                                    ToastTool.toast("停止");
                                    break;
                                case  "AT+KEYON":
                                    ToastTool.toast("触摸键启用");
                                    break;
                                case  "AT+KEYOFF":
                                    ToastTool.toast("触摸键停用");
                                    break;
                                case  "AT+PSSTOP":
                                    ToastTool.toast("排水停止");
                                    break;
                                case  "AT+XD":
                                    ToastTool.toast("消毒开始");
                                    break;
                                case  "AT+CS3":
                                    ToastTool.toast("桶装水容量设置成功");
                                    break;
                                case "AT+CSH":
                                    ToastTool.toast("初始化成功");
                                    msg_send="AT+PS";
                                    writeToUSb(msg_send);
                                    break;
                                case "AT+PS":
                                 //   ToastTool.toast("排水成功");
                                    msg_send="AT+LBKPS";
                                    writeToUSb(msg_send);
                                    break;
                                case "AT+LBKPS":
                                   // ToastTool.toast("凉白开排水成功");
                            }
                        }else  if(str.contains("AT+PSSTOP")){
                            ps_pro=0;
                            paishuiProgress.setProgress(100);
                            ShareTool.saveInfo(PaidShuiActivity.this,"progress",ps_pro+"");
                            ps_handler.removeCallbacks(runnable1);
                        }

                        Log.i(TAG, "排水Message: "+str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
            return false;
        }
    });
    @Override
    protected void onResume() {
        super.onResume();
        WindowTool.hideMenu(this);
        String progress = ShareTool.getInfo(PaidShuiActivity.this, "progress");
        if (progress==null||progress.length()==0){
            progress="0";
        }
        ps_pro=Integer.parseInt(progress);
        ps_handler.postDelayed(runnable1, ps_i);
        if (ps_pro==0) {
            msg_send = "AT+PS";
            writeToUSb(msg_send);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            UsbTool.getInstance().unRegist();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.head_close, R.id.paishui_progress})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_close:
                if(activity!=null&&activity.length()>0){
                    switch (activity){
                        case"set":
                            JumpTool.jump(PaidShuiActivity.this,SetActivity.class);
                            break;
                        case"main":
                            break;
                    }
                }
               finish();
                break;
            case R.id.paishui_progress:

                break;
        }
    }
    /**
     * @param s
     * 往平板写数据
     *
     */
    public void writeToUSb(String s){
        byte[] data = s.getBytes();
        UsbTool.getInstance().writeToUsb(data);
    }
}
